/**
 * Created by longgs on 2018-7-18.
 */
angular.module('MetronicApp').controller('AlgorithmListController',
    ['$rootScope', '$scope', '$state', '$uibModal', '$stateParams', 'toastr', 'AlgorithmService', 'EnumService',
        function ($rootScope, $scope, $state, $uibModal, $stateParams, toastr, AlgorithmService, EnumService) {
            $scope.$on('$viewContentLoaded', function () {
                App.initAjax();
                $rootScope.settings.layout.pageBodySolid = true;
                $rootScope.settings.layout.pageSidebarClosed = false;
            });


            $scope.condition = {};
            $scope.algorithmData = EnumService.get('analysisIndex');
            //获取ng-table表头
            $scope.columns = AlgorithmService.getSchema();

            $scope.pageable = AlgorithmService.getPageable();
            $scope.$watch('pageable.size', function (newVal, oldVal) {
                if (newVal === oldVal) return;
                AlgorithmService.setSize(newVal);
                $scope.list();
            });

            $scope.$watch('pageable.number', function (newVal, oldVal) {
                if (newVal === oldVal) return;
                AlgorithmService.setStoredPage(newVal);
                $scope.list();
            });

            //企业画像表头五个tab数据获取
            $scope.tabItemList = [
                {
                    "name": "标签体系",
                    "router": "labelSystemList",
                    "selected": false
                },
                {
                    "name": "企业信息",
                    "router": "informationList",
                    "selected": false
                },
                {
                    "name": "挖掘算法",
                    "router": "algorithmList",
                    "selected": true
                },
                {
                    "name": "企业画像",
                    "router": "businessPortraitList",
                    "selected": false
                },
                {
                    "name": "标签检索",
                    "router": "labelRetrievalList",
                    "selected": false
                }
        ]
            $scope.list = function () {
                AlgorithmService.putSearchParams({
                    search: $scope.condition.search
                });
                AlgorithmService.list(function (res) {
                    if ('success' === res.status) {
                        $scope.rows = res.data;
                        $scope.pageable = res.pageable;

                    } else {
                        for (var i = 0; i < res.errors.length; i++) {
                            toastr.error(res.errors[i].errmsg, "数据获取失败");
                        }
                    }
                });
            };

            var gotoFirstPage = function () {
                AlgorithmService.setStoredPage(0);
                $scope.list();
                AlgorithmService.clearSearchParams();
            };
            gotoFirstPage();

            //新建
            $scope.create = function () {
                $state.go('algorithmEdit');
            };
            //统计
            $scope.statistic = function () {
                $state.go('algorithmStatistic');
            };

            //编辑
            $scope.edit = function (row) {
                $state.go('algorithmEdit', {id: row.id});
                $scope.edit=true;
            };

            //详情
            $scope.view = function (row) {
                $state.go('algorithmView', {id: row.id});
            };
           // 删除
            $scope.delete = function (row) {

                var modalInstance = $uibModal.open({
                    templateUrl: 'confirm.html',
                    controller: ["$scope", "$uibModalInstance", function (scope, $uibModalInstance) {
                        scope.confirmContent = "确定删除此数据？";
                        scope.btn_ok = function () {
                            $uibModalInstance.close(row.id);
                        };
                        scope.btn_cancel = function () {
                            $uibModalInstance.dismiss();
                        };
                    }]
                });
                modalInstance.result.then(function (id) {

                        AlgorithmService.delete(id).$promise.then(function (result) {
                            if ("success" == result.status) {
                               // code here
                                toastr.success("", "数据删除成功。");
                            } else {
                                for (var index in result.errors) {
                                    toastr.error(result.errors[index].errmsg, "数据删除失败");
                                }
                            }
                        });

                });
            };

            //配置
            // $scope.deploy = function (row) {
            //     $state.go('algorithmDeploy', {id: row.id});
            // };

        }
    ]
);